divert(`-1')

dnl
dnl Predefined macros for loop
dnl
dnl
dnl Macro: foreach(x, (item_1, item_2, ..., item_n), stmt)
dnl 
dnl Example: foreach(`ID', (a, b, c), `print ID')
dnl  
define(`foreach', `pushdef(`$1', `')_foreach(`$1', `$2', `$3')popdef(`$1')')
define(`_arg1', `$1')
define(`_foreach',
        `ifelse(`$2', `()', ,
                `define(`$1', _arg1$2)$3`'_foreach(`$1', (shift$2), `$3')')')
dnl
dnl Macro: forloop(i, from, to, stmt)
dnl 
dnl Example: forloop(`ID', 1, 4, `print ID')
dnl 
define(`forloop', `pushdef(`$1', `$2')_forloop($@)popdef(`$1')')
define(`_forloop', `$4`'ifelse($1, `$3', `', `define(`$1', incr($1))$0($@)')')

dnl
dnl Macro: generalForLoop(i, from, to, op, stmt)
dnl 
define(`generalForLoop',
`ifelse(
  eval($2 > $3),  1,
    `',
  eval($2 <= $3), 1,
    `pushdef(`$1', `$2')$5`'popdef(`$1')')`'dnl
ifelse(
  eval($2 < $3), 1,
    `generalForLoop(`$1', eval($2 $4), `$3', `$4', `$5')')')

divert
